package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.MarketCommodity;
import com.woniuxy.common.entity.MarketOrder;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.MarketCommodityMapper;
import com.woniuxy.common.mapper.MarketOrderMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.MarketOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MarketOrderServiceImpl implements MarketOrderService {
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketCommodityMapper marketCommodityMapper;
    @Autowired
    CenterUserMapper centerUserMapper;

    @Override
    public List<MarketOrder> findAllOrderBySendUserId(String senduserid) {
        List<MarketOrder> marketOrders = marketOrderMapper.selectISellOrder(senduserid);
        for (MarketOrder marketOrder:marketOrders ) {
            MarketCommodity marketCommodity = marketCommodityMapper.selectByPrimaryKey(marketOrder.getCommodityId());
            marketOrder.setCommodity(marketCommodity);
            CenterUser buyUser = centerUserMapper.selectByPrimaryKey(marketOrder.getBuyuserId());
            marketOrder.setBuyuser(buyUser);
        }
        return marketOrders;
    }

    @Override
    public List<MarketOrder> findAllOrderByBuyUserId(String buyuserid) {
        List<MarketOrder> marketOrders = marketOrderMapper.selectIBuyOrder(buyuserid);
        for (MarketOrder marketOrder:marketOrders ) {
            MarketCommodity marketCommodity = marketCommodityMapper.selectByPrimaryKey(marketOrder.getCommodityId());
            marketOrder.setCommodity(marketCommodity);
            CenterUser sendUser = centerUserMapper.selectByPrimaryKey(marketOrder.getSenduserId());
            marketOrder.setSenduser(sendUser);
        }
        return marketOrders;
    }

    @Override
    public MarketOrder findOrderById(String orderid) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderid);
        //赋值
        MarketCommodity marketCommodity = marketCommodityMapper.selectByPrimaryKey(marketOrder.getCommodityId());
        marketOrder.setCommodity(marketCommodity);
        CenterUser sendUser = centerUserMapper.selectByPrimaryKey(marketOrder.getSenduserId());
        marketOrder.setSenduser(sendUser);
        CenterUser buyUser = centerUserMapper.selectByPrimaryKey(marketOrder.getBuyuserId());
        marketOrder.setBuyuser(buyUser);
        return marketOrder;
    }

    @Transactional
    @Override
    public MarketOrder insertOrder(MarketOrder marketOrder) {
        marketOrder.setId(KeyUtil.generateKey());
        marketOrder.setDate(new Date());
        marketOrder.setStatus(0);
        int insert = marketOrderMapper.insert(marketOrder);
        if (insert!=0) {
            int i = marketCommodityMapper.updateStatusById(marketOrder.getCommodityId());
            MarketOrder newMarketOrder = marketOrderMapper.selectByPrimaryKey(marketOrder.getId());
            return  newMarketOrder;
        }
        return null;
    }

    @Override
    public int updateOrderStatusWithPay(String orderId) {
        int i = marketOrderMapper.updatePayStatusById(orderId);
        return i;
    }

    @Override
    public int updateOrderStatusWithSign(String orderId) {
        int i = marketOrderMapper.updateSignStatusById(orderId);
        return i;
    }
}
